addon = LibStub("AceAddon-3.0"):NewAddon("CHBG", "AceConsole-3.0", "AceEvent-3.0")

local defaults = {
	profile = {
		message = "TestMessage",
		showInChat = false,
		showOnScreen = true,
	},
}

local options = {
	name = "CHBG",
	handler = addon,
	type = 'group',
	args = {
		msg = {
			type = "input",
			name = "Message",
			desc = "The message to be displayed when you get home.",
			usage = "<Your message>",
			get = "GetMessage",
			set = "SetMessage",
		},
		showInChat = {
			type = "toggle",
			name = "Show in Chat",
			desc = "Toggles the display of the message in the chat window.",
			get = "IsShowInChat",
			set = "ToggleShowInChat",
		},
		showOnScreen = {
			type = "toggle",
			name = "Show on Screen",
			desc = "Toggles the display of the message on the screen.",
			get = "IsShowOnScreen",
			set = "ToggleShowOnScreen",
		},
	},
}

function addon:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("CHBGDB", defaults, "Default")

	LibStub("AceConfig-3.0"):RegisterOptionsTable("CHBG", options)
	--addon.message = "TestMessage"
	--addon.showInChat = false
	--addon.showOnScreen = true
	self:RegisterChatCommand("chbg","ChatCommand")
	self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("CHBG", "ChainHealBounceGuess")
end

function addon:OnEnable()
	self:Print("HI!")
	self:RegisterEvent("PLAYER_TARGET_CHANGED")
end

function addon:OnDisable()

end

function addon:ChatCommand(input)
	if not input or input:trim() == "" then
		InterfaceOptionsFrame_OpenToCategory(self.optionsFrame)
	else
		LibStub("AceConfigCmd-3.0").HandleCommand(addon, "chbg", "ChainHealBounceGuess", input)
	end
end

function addon:GetMessage(info)
	return self.db.profile.message
end

function addon:SetMessage(info, newValue)
	self.db.profile.message = newValue
end

function addon:IsShowInChat(info)
	return self.db.profile.showInChat
end

function addon:ToggleShowInChat(info, value)
	self.db.profile.showInChat = value
end

function addon:IsShowOnScreen(info)
	return self.db.profile.showOnScreen
end


function addon:ToggleShowOnScreen(info, value)
	self.db.profile.showOnScreen = value
end

function addon:PLAYER_TARGET_CHANGED()
	if self.showInChat then
		self:Print(self.db.profile.message)
	end
	
	if self.showOnScreen then
		UIErrorsFrame:AddMessage(self.db.profile.message, 1.0, 1.0, 1.0, 5.0)
	end
end
